home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4882 / 4882.xpi / chrome / tabscope.jar / content / tabscope / tabscope.xml < prev   
Extensible Markup Language  |  2009-12-03  |  15KB  |  444 lines

  1. <?xml version="1.0" ?>
  2.  
  3. <bindings id="tabscopeBindings"
  4.           xmlns="http://www.mozilla.org/xbl"
  5.           xmlns:xbl="http://www.mozilla.org/xbl"
  6.           xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  7.           xmlns:html="http://www.w3.org/1999/xhtml">
  8.  
  9.     <binding id="tabscope-popup" extends="chrome://global/content/bindings/popup.xml#popup">
  10.  
  11.         <resources>
  12.             <stylesheet src="chrome://tabscope/skin/tabscope.css" />
  13.         </resources>
  14.  
  15.         <content ignorekeys="true" noautofocus="true">
  16.             <xul:label anonid="tabscope-label" class="tabscope-label" crop="end" />
  17.             <xul:hbox anonid="tabscope-toolbar">
  18.                 <xul:toolbarbutton anonid="tabscope-back"    class="tabscope-back" />
  19.                 <xul:toolbarbutton anonid="tabscope-forward" class="tabscope-forward" />
  20.                 <xul:toolbarbutton anonid="tabscope-reload"  class="tabscope-reload" />
  21.                 <xul:toolbarbutton anonid="tabscope-stop"    class="tabscope-stop" />
  22.                 <xul:toolbarbutton anonid="tabscope-zoom"    class="tabscope-zoom" />
  23.                 <xul:toolbarbutton anonid="tabscope-close"   class="tabscope-close" />
  24.             </xul:hbox>
  25.             <xul:hbox anonid="tabscope-canvas-box" class="tabscope-canvas-box">
  26.                 <html:canvas anonid="tabscope-canvas" class="tabscope-canvas" />
  27.             </xul:hbox>
  28.         </content>
  29.  
  30.         <implementation implements="nsIDOMEventListener,nsITimerCallback">
  31.  
  32.             <!-- xul:tabbrowser -->
  33.             <field name="_tabbrowser">null</field>
  34.  
  35.             <!-- xul:tab -->
  36.             <field name="_tab">null</field>
  37.  
  38.             <!-- xul:browser -->
  39.             <field name="_browser">null</field>
  40.  
  41.             <!-- timer id to show popup -->
  42.             <field name="_showTimerID">null</field>
  43.  
  44.             <!-- nsITimer to refresh popup -->
  45.             <field name="_refreshTimer">null</field>
  46.  
  47.             <!-- last time when hiding popup -->
  48.             <field name="_lastTimeToHide">0</field>
  49.  
  50.             <!-- cache of prefs -->
  51.             <field name="_prefs">null</field>
  52.  
  53.             <!-- quick access to canvas -->
  54.             <field name="canvas">null</field>
  55.  
  56.             <!-- mode of preview scale -->
  57.             <field name="_scaleMode">1</field>
  58.  
  59.             <!-- nsIWebProgressListener -->
  60.             <field name="_progressListener"><![CDATA[
  61.                 ({
  62.                     _shouldRefresh: false,
  63.                     onLocationChange: function() { this._shouldRefresh = true; },
  64.                     onProgressChange: function() { this._shouldRefresh = true; },
  65.                     onSecurityChange: function() { this._shouldRefresh = true; },
  66.                     onStateChange   : function() { this._shouldRefresh = true; },
  67.                     onStatusChange  : function() { this._shouldRefresh = true; },
  68.                     QueryInterface: function(aIID) {
  69.                         const Ci = Components.interfaces;
  70.                         if (aIID.equals(Ci.nsIWebProgressListener) ||
  71.                             aIID.equals(Ci.nsISupportsWeakReference) ||
  72.                             aIID.equals(Ci.nsISupports))
  73.                             return this;
  74.                         throw Components.results.NS_NOINTERFACE;
  75.                     }
  76.                 })
  77.             ]]></field>
  78.  
  79.             <method name="log">
  80.                 <parameter name="aMessage" />
  81.                 <parameter name="aAddTime" />
  82.                 <body><![CDATA[
  83.                 dump("tabscope" + (aAddTime ? "@" + new Date().toLocaleTimeString() : "") + "> " + aMessage + "\n");
  84.                 ]]></body>
  85.             </method>
  86.  
  87.             <method name="getElement">
  88.                 <parameter name="aAnonymousID"/>
  89.                 <body><![CDATA[
  90.                 return document.getAnonymousElementByAttribute(this, "anonid", aAnonymousID)
  91.                 ]]></body>
  92.             </method>
  93.  
  94.             <method name="abort">
  95.                 <body><![CDATA[
  96.                 if (this._showTimerID) {
  97.                     window.clearTimeout(this._showTimerID);
  98.                     this._showTimerID = null;
  99.                 }
  100.                 if (this._tab)
  101.                     this.hidePopup();
  102.                 this._tab = null;
  103.                 ]]></body>
  104.             </method>
  105.  
  106.             <method name="refresh">
  107.                 <body><![CDATA[
  108.                 this.getElement("tabscope-back").disabled    = !this._browser.canGoBack;
  109.                 this.getElement("tabscope-forward").disabled = !this._browser.canGoForward;
  110.                 this.getElement("tabscope-stop").disabled    = !this._browser.webProgress.isLoadingDocument;
  111.                 this.getElement("tabscope-label").value = this._tab.label;
  112.                 this.getElement("tabscope-label").setAttribute("tooltiptext", this._tab.label);
  113.                 var win = this._browser.contentWindow;
  114.                 var empty = win.location.href == "about:blank";
  115.                 var showTitle = empty || this._prefs["show_title"];
  116.                 this.getElement("tabscope-label").style.display = showTitle ? "block" : "none";
  117.                 this.getElement("tabscope-toolbar").style.display = empty ? "none" : "block";
  118.                 this.getElement("tabscope-canvas-box").style.display = empty ? "none" : "block";
  119.                 this.getElement("tabscope-label").width = empty ? null : this.canvas.width;
  120.                 if (empty)
  121.                     return;
  122.                 var w, h;
  123.                 var s = this.canvas.scale || this._prefs["preview_scale"];
  124.                 if (s == 0) {
  125.                     w = win.innerWidth;
  126.                     s = this.canvas.width / w;
  127.                     h = this.canvas.height / s;
  128.                 }
  129.                 else {
  130.                     w = this.canvas.width / s;
  131.                     h = this.canvas.height / s;
  132.                 }
  133.                 var ctx = this.canvas.getContext("2d");
  134.                 ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
  135.                 ctx.save();
  136.                 ctx.scale(s, s);
  137.                 ctx.drawWindow(win, win.scrollX, win.scrollY, w, h, "rgb(255,255,255)");
  138.                 ctx.restore();
  139.                 ]]></body>
  140.             </method>
  141.  
  142.             <method name="toggleScale">
  143.                 <body><![CDATA[
  144.                 if (this._scaleMode == 1) {
  145.                     this._scaleMode = 2;
  146.                     this.canvas.width  = this._prefs["preview_width.2"];
  147.                     this.canvas.height = this._prefs["preview_height.2"];
  148.                     this.canvas.scale  = this._prefs["preview_scale.2"];
  149.                 }
  150.                 else {
  151.                     this._scaleMode = 1;
  152.                     this._initCanvasWithPrefs();
  153.                 }
  154.                 this.refresh();
  155.                 ]]></body>
  156.             </method>
  157.  
  158.             <method name="_handleMouseOver">
  159.                 <parameter name="aEvent" />
  160.                 <body><![CDATA[
  161.                 if (this._showTimerID || this._refreshTimer)
  162.                     return;
  163.                 var allTabs = false;
  164.                 if (aEvent.target.localName == "tab") {
  165.                     if (aEvent.target == this._tab)
  166.                         return;
  167.                     if (!this._prefs["current_tab"] && aEvent.target == this._tabbrowser.mCurrentTab) {
  168.                         this.abort();
  169.                         return;
  170.                     }
  171.                 }
  172.                 else if (aEvent.originalTarget.localName == "menuitem" && aEvent.originalTarget.tab) {
  173.                     allTabs = true;
  174.                 }
  175.                 else {
  176.                     return;
  177.                 }
  178.                 this._tab  = !allTabs ? aEvent.target : aEvent.originalTarget.tab;
  179.                 var anchor = !allTabs ? aEvent.target : aEvent.originalTarget;
  180.                 var callback = function(self) {
  181.                     var position = "";
  182.                     if (!allTabs) {
  183.                         switch (self._prefs["popup_alignment"]) {
  184.                             case 1: position = "before_start"; break;
  185.                             case 2: position = "after_start"; break;
  186.                             case 3: position = "start_before"; break;
  187.                             case 4: position = "end_before"; break;
  188.                         }
  189.                     }
  190.                     else
  191.                         position = (self._prefs["popup_alignment"] == 1) ? "end_after" : "end_before";
  192.                     self.openPopup(anchor, position, 0, 0, true, false);
  193.                 };
  194.                 var delay = this._prefs["continuously"] && (Date.now() - this._lastTimeToHide <= 50)
  195.                           ? 10 : this._prefs["popup_delay"];
  196.                 this._showTimerID = window.setTimeout(callback, delay, this);
  197.                 ]]></body>
  198.             </method>
  199.  
  200.             <method name="_handleMouseOut">
  201.                 <parameter name="aEvent" />
  202.                 <body><![CDATA[
  203.                 var rel = aEvent.relatedTarget;
  204.                 if (rel) {
  205.                     while (rel) {
  206.                         if (rel == this._tab || rel == this)
  207.                             return;
  208.                         rel = rel.parentNode;
  209.                     }
  210.                     this.abort();
  211.                     return;
  212.                 }
  213.                 var x = aEvent.screenX;
  214.                 var y = aEvent.screenY;
  215.                 if (this._refreshTimer) {
  216.                     if (this._isEntering(x, y, this, false) || 
  217.                         this._isEntering(x, y, this._tab, true))
  218.                         return;
  219.                 }
  220.                 this.abort();
  221.                 ]]></body>
  222.             </method>
  223.  
  224.             <method name="_isEntering">
  225.                 <parameter name="aScreenX" />
  226.                 <parameter name="aScreenY" />
  227.                 <parameter name="aElement" />
  228.                 <parameter name="aAllowOnEdge" />
  229.                 <body><![CDATA[
  230.                 var x = aElement.boxObject.screenX;
  231.                 var y = aElement.boxObject.screenY;
  232.                 var c = aAllowOnEdge ? 1 : 0;
  233.                 if (x < aScreenX - c && aScreenX < x + aElement.boxObject.width + c && 
  234.                     y < aScreenY - c && aScreenY < y + aElement.boxObject.height + c) {
  235.                     return true;
  236.                 }
  237.                 return false;
  238.                 ]]></body>
  239.             </method>
  240.  
  241.             <method name="readPrefs">
  242.                 <body><![CDATA[
  243.                 var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
  244.                                  .getService(Components.interfaces.nsIPrefService);
  245.                 prefBranch = prefBranch.getBranch("extensions.tabscope.");
  246.                 this._prefs = {
  247.                     "popup_alignment"  : prefBranch.getIntPref("popup_alignment"),
  248.                     "popup_delay"      : prefBranch.getIntPref("popup_delay"),
  249.                     "continuously"     : prefBranch.getBoolPref("continuously"),
  250.                     "current_tab"      : prefBranch.getBoolPref("current_tab"),
  251.                     "show_title"       : prefBranch.getBoolPref("show_title"),
  252.                     "preview_width"    : prefBranch.getIntPref("preview_width"),
  253.                     "preview_height"   : prefBranch.getIntPref("preview_height"),
  254.                     "preview_scale"    : prefBranch.getIntPref("preview_scale") / 100,
  255.                     "preview_width.2"  : prefBranch.getIntPref("preview_width.2"),
  256.                     "preview_height.2" : prefBranch.getIntPref("preview_height.2"),
  257.                     "preview_scale.2"  : prefBranch.getIntPref("preview_scale.2") / 100,
  258.                 };
  259.                 this._initCanvasWithPrefs();
  260.                 this.style.MozBoxDirection = (this._prefs["popup_alignment"] == 1) ? "reverse" : "";
  261.                 ]]></body>
  262.             </method>
  263.  
  264.             <method name="_initCanvasWithPrefs">
  265.                 <body><![CDATA[
  266.                 this.canvas.width  = this._prefs["preview_width"];
  267.                 this.canvas.height = this._prefs["preview_height"];
  268.                 this.canvas.scale  = this._prefs["preview_scale"];
  269.                 ]]></body>
  270.             </method>
  271.  
  272.             <method name="_getActualElementAndPoint">
  273.                 <parameter name="aScreenX" />
  274.                 <parameter name="aScreenY" />
  275.                 <body><![CDATA[
  276.                 var win = this._browser.contentWindow;
  277.                 var box = document.getBoxObjectFor(this.canvas);
  278.                 var x = aScreenX - box.screenX;
  279.                 var y = aScreenY - box.screenY;
  280.                 var css = window.getComputedStyle(this.canvas, null);
  281.                 x -= parseInt(css.marginLeft, 10) + parseInt(css.borderLeftWidth, 10) + parseInt(css.paddingLeft, 10);
  282.                 y -= parseInt(css.marginTop, 10)  + parseInt(css.borderTopWidth, 10)  + parseInt(css.paddingTop, 10);
  283.                 var scale = this.canvas.scale || this.canvas.width / win.innerWidth;
  284.                 x = Math.round(x / scale);
  285.                 y = Math.round(y / scale);
  286.                 var elt = win.document.elementFromPoint(x, y);
  287.                 var nest = 0;
  288.                 while (elt.localName.toLowerCase() == "iframe" || elt.localName.toLowerCase() == "frame") {
  289.                     if (++nest > 10)
  290.                         break;
  291.                     x -= elt.getBoundingClientRect().left;
  292.                     y -= elt.getBoundingClientRect().top;
  293.                     elt = elt.contentDocument.elementFromPoint(x, y);
  294.                 }
  295.                 return [elt, x, y];
  296.                 ]]></body>
  297.             </method>
  298.  
  299.             <constructor><![CDATA[
  300.                 this.canvas = this.getElement("tabscope-canvas");
  301.                 this.readPrefs();
  302.                 this._tabbrowser = document.getElementById(this.getAttribute("browserid"));
  303.                 this._tabbrowser.mStrip.tooltip = null;
  304.                 this._tabbrowser.mStrip.addEventListener("mouseover", this, false);
  305.                 this._tabbrowser.mStrip.addEventListener("mouseout", this, false);
  306.                 this._tabbrowser.mStrip.addEventListener("click", this, false);
  307.                 setTimeout(function(self){
  308.                     self._tabbrowser.mTabContainer.addEventListener("TabSelect", self, false);
  309.                     self._tabbrowser.mTabContainer.addEventListener("TabClose", self, false);
  310.                 }, 100, this);
  311.             ]]></constructor>
  312.  
  313.             <destructor><![CDATA[
  314.                 this.canvas = null;
  315.                 this._tabbrowser = null;
  316.             ]]></destructor>
  317.  
  318.             <!-- nsIDOMEventListener -->
  319.             <method name="handleEvent">
  320.                 <parameter name="aEvent" />
  321.                 <body><![CDATA[
  322.                 switch (aEvent.type) {
  323.                     case "mouseover": 
  324.                         this._handleMouseOver(aEvent);
  325.                         break;
  326.                     case "mouseout": 
  327.                         this._handleMouseOut(aEvent);
  328.                         break;
  329.                     default: 
  330.                         this.abort();
  331.                         break;
  332.                 }
  333.                 ]]></body>
  334.             </method>
  335.  
  336.             <!-- nsITimerCallback -->
  337.             <method name="notify">
  338.                 <parameter name="aTimer" />
  339.                 <body><![CDATA[
  340.                 if (this._progressListener._shouldRefresh) {
  341.                     this._progressListener._shouldRefresh = false;
  342.                     this.refresh();
  343.                 }
  344.                 ]]></body>
  345.             </method>
  346.  
  347.         </implementation>
  348.  
  349.         <handlers>
  350.  
  351.             <handler event="popupshowing" phase="capturing"><![CDATA[
  352.                 if (this._refreshTimer) {
  353.                     this._refreshTimer.cancel();
  354.                     this._refreshTimer = null;
  355.                 }
  356.                 this._refreshTimer = Components.classes["@mozilla.org/timer;1"]
  357.                                      .createInstance(Components.interfaces.nsITimer);
  358.                 this._refreshTimer.initWithCallback(this, 500, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
  359.                 this._browser = this._tabbrowser.getBrowserForTab(this._tab);
  360.                 this._browser.addProgressListener(this._progressListener);
  361.                 this.refresh();
  362.             ]]></handler>
  363.  
  364.             <handler event="popuphiding" phase="capturing"><![CDATA[
  365.                 this._lastTimeToHide = Date.now();
  366.                 this._refreshTimer.cancel();
  367.                 this._refreshTimer = null;
  368.                 this._browser.removeProgressListener(this._progressListener);
  369.                 this._browser = null;
  370.                 this._tab = null;
  371.                 if (gContextMenu)
  372.                     gContextMenu.menu.hidePopup();
  373.                 var tabMenu = document.getAnonymousElementByAttribute(this._tabbrowser, "anonid", "tabContextMenu");
  374.                 tabMenu.hidePopup();
  375.             ]]></handler>
  376.  
  377.             <handler event="mouseout" phase="capturing"><![CDATA[
  378.                 this._handleMouseOut(event);
  379.             ]]></handler>
  380.  
  381.             <handler event="command"><![CDATA[
  382.                 switch (event.originalTarget.getAttribute("anonid")) {
  383.                     case "tabscope-back"   : this._browser.goBack();    break;
  384.                     case "tabscope-forward": this._browser.goForward(); break;
  385.                     case "tabscope-reload" : this._browser.reload();    break;
  386.                     case "tabscope-stop"   : this._browser.stop();      break;
  387.                     case "tabscope-zoom"   : this.toggleScale();  break;
  388.                     case "tabscope-close"  : 
  389.                         this._tabbrowser.removeTab(this._tab);
  390.                         this.abort();
  391.                         var popup = this._tabbrowser.mTabContainer.mAllTabsPopup;
  392.                         if (popup && popup.hasChildNodes())
  393.                             popup.hidePopup();
  394.                         break;
  395.                     default: 
  396.                 }
  397.             ]]></handler>
  398.  
  399.             <handler event="click" button="0"><![CDATA[
  400.                 var targetName = event.originalTarget.localName;
  401.                 if (targetName == "label" || targetName == "popup") {
  402.                     this._tabbrowser.selectedTab = this._tab;
  403.                     this.abort();
  404.                 }
  405.                 else if (targetName == "canvas") {
  406.                     var [elt, x, y] = this._getActualElementAndPoint(event.screenX, event.screenY);
  407.                     if (!elt)
  408.                         return;
  409.                     var evt = elt.ownerDocument.createEvent("MouseEvents");
  410.                     evt.initMouseEvent(
  411.                         event.type, true, false, elt.ownerDocument.defaultView, event.detail,
  412.                         event.screenX, event.screenY, x, y,
  413.                         event.ctrlKey, event.altKey, event.shiftKey, event.metaKey,
  414.                         event.button, null
  415.                     );
  416.                     elt.dispatchEvent(evt);
  417.                     this.refresh();
  418.                 }
  419.             ]]></handler>
  420.  
  421.             <handler event="click" button="1"><![CDATA[
  422.                 this.abort();
  423.             ]]></handler>
  424.  
  425.             <handler event="click" button="2"><![CDATA[
  426.                 if (this.getElement("tabscope-canvas-box").style.display == "none")
  427.                     return;
  428.                 this.toggleScale();
  429.             ]]></handler>
  430.  
  431.             <handler event="DOMMouseScroll" phase="capturing"><![CDATA[
  432.                 var [elt, , ] = this._getActualElementAndPoint(event.screenX, event.screenY);
  433.                 if (!elt)
  434.                     return;
  435.                 elt.ownerDocument.defaultView.scrollBy(0, event.detail < 0 ? -100 : 100);
  436.                 this.refresh();
  437.             ]]></handler>
  438.  
  439.         </handlers>
  440.  
  441.     </binding>
  442.  
  443. </bindings>
  444.